﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_DAL;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller.Decision.Fondos
{
    [RoutePrefix("api/dfdailymain")]
    public class DF_DailyMainController : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "GetdfdailymainByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
        public IHttpActionResult Get(string road = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<Df_Dailymian_Cost, bool>> exp = null;
            //if (road != null && road.ToString().Length > 0)
            //    exp = pre => pre.Rdwayid == road.ToString();
           //exp=pre=>pre.Rd_Class.OrderBy()

            var data = this.Scene.Bll.Df_Dailymian_Cost.Get<Df_Dailymian_Cost>(exp, info).ToArray();
            if (data == null) return NotFound();

            var resdata = from daily in data
                          orderby daily.Pvmttype, daily.Rd_Class, daily.MinYear
                          select new
                          {
                              Id=daily.Id,
                              Pvmttype=daily.Pvmttype,
                              Rd_Class=daily.Rd_Class,
                              Year = daily.MinYear + "<=年限<" + daily.MaxYear,
                              Daily_Unitcost=daily.Daily_Unitcost
                          };

            var result = new { rows = resdata, total = info.TotalRecords };

            return Ok(result);
        }


        [Route("")]
        public HttpResponseMessage Post(Df_Dailymian_Cost entity)
        {
            //entity.Importdate = DateTime.Now;
            //this.Scene.Bll.Df_Dailymian_Cost.Save(entity);

            //Request.getParameterValues

            ISthpms1Dao dao = this.Scene.Dal.Sthpms1;
            dao.Update<Df_Dailymian_Cost>(new { Daily_Unitcost = entity.Daily_Unitcost }, p => p.Id == entity.Id);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetPrevenDetailByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }


    }
}